from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from utils.db import db
import json

# Create your views here.


def showuseraudit(request):

    sql = "select c.id,c.name,c.tmoney,c.company_name,c.status from customer as c left join reg_form as rf on c.refromid=rf.id left join customer_manager as cm on cm.id=c.manager_id"

    res = db.get_all(sql)

    return JsonResponse({"code":200,"list":res})


def audit(request):

    import time
    # 获取参数
    data = json.loads(request.body)
    print(data)
    time = int(time.time())
    sql = "select * from customer where id=%d"%(int(data['id']))
    customer = db.get_one(sql)

    # 更新customer表，状态，审核人，审核时间
    sql1 =  "update customer set status=%d,audit_time=%d,audit_name='zs' where id=%d"%(int(data['is_agree']),time,int(data['id']))
    db.update(sql1)

    # 更改来源表
    sql2 = "select id,tcount from reg_form where id=%d"%(int(customer['refromid']))

    regform = db.get_one(sql2)

    number = int(regform['tcount'])+1

    sql3 = 'update reg_form set tcount=%d where id=%d'%(int(number),int(customer['refromid']))
    db.update(sql3)

    # 更新客户经理中的总金额
    sql4 = "select * from customer_manager where id=%d"%(int(customer['manager_id']))
    manager = db.get_one(sql4)

    number = int(manager['tmoney'])+int(customer['tmoney'])

    sql5 = " update customer_manager set tmoney=%d where id=%d " % (int(number),int(customer['manager_id']))

    db.update(sql5)

    return JsonResponse({"code":200})